/**
 * Created by thinkjoy on 2017/7/20.
 */

layui.use('form', function(){
    var form = layui.form;
    var sex=$('#inputsex').val();
    $('[name="sex"][value="' + sex + '"]').prop('checked', true);//改变radio选择
    form.render();
    //监听提交
    form.on('submit(confirmdata)', function(data){
        // layer.msg(JSON.stringify(data.field));
        $.ajax({
            	url:'/admin/person/updateperson',
            	type:'post',
            	data:data.field,
            	dataType:'json',
            	success:function (e) {
                    if(e=='0'){
                        layer.msg("修改成功");
                        window.location.href="/admin/person/personlist";
                    }else{
                        layer.msg("修改失败，请重试");
                    }
                }
        });
        return false;
    });
});

// 上传图片
layui.use('upload', function(){
    var upload = layui.upload;
    upload.render({
        url: '/admin/person/uploadmyphoto',
        elem: '#uploadfile',
        method: 'post',
        accept:'images',
        done: function(res){
            var responseObj=eval('('+'res'+')');
            var code=responseObj.code;
            if(code==0){
                var photourl=responseObj.data.access_url;
                $('#personphoto').attr('src',photourl);
                $('#photo').val(photourl);
                layer.msg("上传成功");
            }else{
                layer.msg("上传失敗");
            }
        }
    });
});

$(function () {
   $('#updatepwbtn').click(function () {
       var content=$('#layercontent');
        layer.open({
            type: 1,
            skin:'layui-layer-molv',
            title:  ['修改密码', 'font-size:18px;'],
            closeBtn: 2,
            content:content,
            area:  ['680px', '350px'],
            shade: 0.8,
            shadeClose:true,
            anim:1,
            id: 'LAY_layuipro', //设定一个id，防止重复弹出
            btn: ['绝不后悔', '我怂了'],
            moveType: 1,
            success:function (layero, index) {
                $('#oldpassword').val('');
                $('#newpassword').val('');
                $('#checkpassword').val('');
            },
            // 按钮一的回调
            yes:function (index, layero) {
                var oldpwd=$('#oldpassword').val();
                var newpwd=$('#newpassword').val();
                var checkpwd=$('#checkpassword').val();
                if(oldpwd=='' || newpwd=='' || checkpwd==''){
                    layer.msg('信息不全，请完善！');
                }else{
                    if(newpwd!=checkpwd){
                        layer.msg('新密码和确认密码要相同，请重新输入确认密码');
                    }else{
                        var params=$.param({
                                oldpwd:oldpwd,
                                newpwd:newpwd
                               },true);
                        $.ajax({
                            url:'/admin/person/updatepwd',
                            type:'post',
                            data:params,
                            dataType:'json',
                            success:function (e) {
                                if(e=='0'){
                                    layer.msg('密码修改成功,下次请用新密码登录');
                                    layer.close(index);
                                }else if(e=='1'){
                                    layer.msg('密码修改失败');
                                }else if(e=='2'){
                                    layer.msg('旧密码验证不通过，请重新输入');
                                }
                            }
                        })
                    }
                }
            }
        });
   })
});





